package lx.scrmHn;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import lx.util.ConnectionUrlUtil;
import lx.util.StrUtil;

import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimerTask;

/**
 * @Description: 湖南线上token定时器
 * @Author: 李星 lixing_java@163.com
 * @Date: 2020/3/14 10:47
 */
public class HnTokenTask extends TimerTask {
    String tokenUrlForTxl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww19c129aff303a9bf&corpsecret=2MNASv0Zwn1ryVlc3jS2I4O0SPxbeLrlFk25QtkCT8U";
    String tokenUrlForWblxr = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww19c129aff303a9bf&corpsecret=PT6J8RfLTc1WpwD1TThT8PjZ1zmaHgWC6CSci-3s_Co";

    @Override
    public void run() {
        // 删除所有token
        System.out.println(MessageFormat.format("删除token结果：{0}",Db.delete("DELETE FROM HL_SCRM_TOKEN WHERE CATEGORY IN (1,2)")));
        //
        getTokenUrlForTxl(tokenUrlForTxl);
        getTokenUrlForWblxr(tokenUrlForWblxr);
    }

    public void getTokenUrlForTxl(String tokenUrlForTxl) {
        System.out.println("TokenTask----tokenUrlForTxl："+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        System.out.println();

        String result_tokenUrlForTxl = ConnectionUrlUtil.sendGET(tokenUrlForTxl, "");
        System.out.println(MessageFormat.format("getTokenUrlForTxl 从微信服务器获取Token返回值：{0}", result_tokenUrlForTxl));

        if (StrUtil.isEffective(result_tokenUrlForTxl)) {
            try {
                JSONObject result = JSONObject.parseObject(result_tokenUrlForTxl);
                if ("0".equals(result.getString("errcode")) && "ok".equals(result.getString("errmsg"))) {
                    String access_token = result.getString("access_token");
                    int expires_in = result.getInteger("expires_in");
                    //
                    Record record_tokenUrlForTxl = new Record();
                    record_tokenUrlForTxl.set("ACCESS_TOKEN", access_token);
                    record_tokenUrlForTxl.set("EXPIRES_IN", expires_in);
                    record_tokenUrlForTxl.set("CATEGORY", 1); // TODO 通讯录
                    boolean flag = Db.save("HL_SCRM_TOKEN",record_tokenUrlForTxl);
                    System.out.println(MessageFormat.format("getTokenUrlForTxl添加到本地：{0}", flag));
                } else {
                    System.out.println(MessageFormat.format("getTokenUrlForTxl 从微信服务器获取Token失败：{0}", JSONObject.toJSONString(result)));
                }
            } catch (Exception e) {
                System.out.println(MessageFormat.format("getTokenUrlForTxl 从微信服务器获取Token失败：{0}", e.getMessage()));
            }
        } else {
            System.out.println("getTokenUrlForTxl 返回值为空");
        }
    }

    public void getTokenUrlForWblxr(String tokenUrlForWblxr) {
        System.out.println("TokenTask----tokenUrlForWblxr："+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        System.out.println();

        String result_tokenUrlForWblxr = ConnectionUrlUtil.sendGET(tokenUrlForWblxr, "");
        System.out.println(MessageFormat.format("tokenUrlForWblxr 从微信服务器获取Token返回值：{0}", result_tokenUrlForWblxr));

        if (StrUtil.isEffective(result_tokenUrlForWblxr)) {
            try {
                JSONObject result = JSONObject.parseObject(result_tokenUrlForWblxr);
                if ("0".equals(result.getString("errcode")) && "ok".equals(result.getString("errmsg"))) {
                    String access_token = result.getString("access_token");
                    int expires_in = result.getInteger("expires_in");
                    //
                    Record record_tokenUrlForTxl = new Record();
                    record_tokenUrlForTxl.set("ACCESS_TOKEN", access_token);
                    record_tokenUrlForTxl.set("EXPIRES_IN", expires_in);
                    record_tokenUrlForTxl.set("CATEGORY", 2); // TODO 外部联系人
                    boolean flag = Db.save("HL_SCRM_TOKEN",record_tokenUrlForTxl);
                    System.out.println(MessageFormat.format("tokenUrlForWblxr：{0}", flag));
                } else {
                    System.out.println(MessageFormat.format("tokenUrlForWblxr 从微信服务器获取Token失败：{0}", JSONObject.toJSONString(result)));
                }
            } catch (Exception e) {
                System.out.println(MessageFormat.format("tokenUrlForWblxr 从微信服务器获取Token失败：{0}", e.getMessage()));
            }
        } else {
            System.out.println("tokenUrlForWblxr 返回值为空");
        }
    }

}
